package com.yupi.project.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class DirectProducer {

  private static final String EXCHANGE_NAME = "direct_logs";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    try (Connection connection = factory.newConnection();
         Channel channel = connection.createChannel();
         Scanner scanner = new Scanner(System.in)) {
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        while (scanner.hasNextLine()) {
            String input = scanner.nextLine();
            String[] split = input.split(" ");
            String message = split[0];
            String rountine = split[1];
            channel.basicPublish(EXCHANGE_NAME, rountine, null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + rountine + "':'" + message + "'");
        }
    }
  }
  //..
}